/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.Tips;
import top.continew.admin.hanfu.domain.bo.TipsBo;
import top.continew.admin.hanfu.domain.dto.TipsDto;
import top.continew.admin.hanfu.domain.query.TipsQuery;
import top.continew.admin.hanfu.domain.vo.TipsVo;
import top.continew.admin.hanfu.domain.vo.wx.TipsWxVo;
import top.continew.admin.hanfu.service.impl.TipsServiceImpl;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-04-15
 */
@RestController
@RequestMapping("/tips")
@RequiredArgsConstructor
public class TipsController {

    private final TipsServiceImpl tipsService;

    @PostMapping("")
    public R<Void> addTips(@RequestBody TipsDto tipsDto) {

        Tips tips = BeanUtil.copyProperties(tipsDto, Tips.class);

        tips.setUpdateTime(String.valueOf(System.currentTimeMillis()));

        tipsService.addTips(tips);

        return R.success();
    }

    @DeleteMapping("/{id}")
    public R<Void> delTips(@PathVariable("id") Integer id) {
        tipsService.delTips(id);
        return R.success();
    }

    @PutMapping("")
    public R<Void> putTips(@RequestBody TipsDto tipsDto) {
        Tips tips = BeanUtil.copyProperties(tipsDto, Tips.class);
        tips.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        tipsService.putTips(tips);
        return R.success();
    }

    @PostMapping("/get")
    public R<Page<TipsVo>> getTipsPage(@RequestBody TipsQuery tipsQuery) {

        TipsBo tipsBo = new TipsBo();

        tipsBo.setTitle(tipsQuery.getTitle().isEmpty() ? null : tipsQuery.getTitle());
        tipsBo.setStatus(tipsQuery.getStatus() == null ? null : tipsQuery.getStatus());

        Page<TipsVo> tipsVoPage = tipsService.getTipsPage(new Page<>(tipsQuery.getPageNum(), tipsQuery
            .getPageSize()), tipsBo);

        tipsVoPage.getRecords().forEach(tipsVo -> {
            tipsVo.setUpdateTime(DateUtil.date(Long.parseLong(tipsVo.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(tipsVoPage);
    }

    @SaIgnore
    @PostMapping("/wx/get")
    public R<Page<TipsWxVo>> getTipsWxPage(@RequestBody TipsQuery tipsQuery) {
        TipsBo tipsBo = new TipsBo();
        tipsBo.setTitle(tipsQuery.getTitle().isEmpty() ? null : tipsQuery.getTitle());
        tipsBo.setStatus(tipsQuery.getStatus() == null ? null : tipsQuery.getStatus());
        Page<TipsWxVo> tipsVoPage = tipsService.getTipsWxPage(new Page<>(tipsQuery.getPageNum(), tipsQuery
            .getPageSize()), tipsBo);
        tipsVoPage.getRecords().forEach(tipsVo -> {
            tipsVo.setUpdateTime(DateUtil.date(Long.parseLong(tipsVo.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(tipsVoPage);
    }

}
